/**
 *
 */
package com.angel.io.type.cols;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.angel.common.helpers.StringHelper;
import com.angel.io.exceptions.AliasNameRepeteadException;

/**
 * @author William
 *
 */
public class HeaderColumnFile{

	private Integer position;
	private String columnName;
	private List<String> alias;
	private boolean unique;

	public HeaderColumnFile(String name, Collection<String> alias){
		super();
		this.unique = false;
		this.columnName = name;
		this.alias = new ArrayList<String>();
		this.alias.addAll(alias);
	}
	
	public HeaderColumnFile(String name, Collection<String> alias, boolean unique){
		super();
		this.unique = unique;
		this.columnName = name;
		this.alias = new ArrayList<String>();
		this.alias.addAll(alias);
	}

	public HeaderColumnFile(String name){
		super();
		this.columnName = name;
		this.alias = new ArrayList<String>();
	}

	/**
	 * @return the columnName
	 */
	public String getColumnName() {
		return columnName;
	}

	/**
	 * @param columnName the columnName to set
	 */
	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	/**
	 * @return the alias
	 */
	public List<String> getAlias() {
		return alias;
	}

	/**
	 * @param alias the alias to set
	 */
	public void setAlias(List<String> alias) {
		this.alias = alias;
	}

	public void addAllAliasName(Collection<String> aliasNames){
		for(String a: aliasNames){
			if(!this.containsAlias(a)){
				this.alias.add(a);
			}
		}
	}

	public void addAliasName(String aliasName){
		if(!this.containsAlias(aliasName)){
			this.alias.add(aliasName);
		} else {
			String columnNames = "";
			for(String a: this.alias){
				columnNames = columnNames.concat(a + " - ");
			}
			throw new AliasNameRepeteadException("Alias to adds [" + aliasName + "] is repetead [" + columnNames + "] at column [ Name: " + this.getColumnName() + " - Pos.: " + this.getPosition() + "].");
		}
	}

	public boolean containsAlias(String aliasName){
		return this.alias.contains(aliasName);
	}

	/**
	 * @return the position
	 */
	public Integer getPosition() {
		return position;
	}

	/**
	 * @param position the position to set
	 */
	public void setPosition(Integer position) {
		this.position = position;
	}

	public boolean isNamed(String columnName) {
		boolean named = false;
		if(this.getColumnName() != null && this.getColumnName().equalsIgnoreCase(columnName)){
			named = true;
		}
		if(named != true){
			for(String alias: this.alias){
				if(alias.equalsIgnoreCase(columnName)){
					named = true;
					return named;
				}
			}
		}
		return named;
	}

	@Override
	public String toString(){
		String string = this.getColumnName() + "(" + this.getPosition() + "|" + this.getAllAliasNamesConcated() + ")";
		return string;
	}

	private String getAllAliasNamesConcated(){
		String aliasNames = "";
		for(String s: this.alias){
			if(this.alias.get(this.alias.size() - 1).equals(s)){
				aliasNames = aliasNames.concat(s.toString());
			} else {
				aliasNames = aliasNames.concat(s.toString() + " - ");
			}
		}
		return !StringHelper.isEmpty(aliasNames) ? aliasNames : "No Alias.";
	}

	/**
	 * @return the unique
	 */
	public boolean isUnique() {
		return unique;
	}

	/**
	 * @param unique the unique to set
	 */
	public void setUnique(boolean unique) {
		this.unique = unique;
	}
}
